package com.gdut.simple.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gdut.simple.R;
import com.gdut.simple.common.DateUtils;
import com.gdut.simple.data.entity.Comment;
import com.gdut.simple.view.adapter.base.AbsRecyclerAdapter;
import com.gdut.simple.view.adapter.base.AbsViewHolder;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.bmob.v3.datatype.BmobFile;

public class CommentAdapter extends AbsRecyclerAdapter<Comment> {


    public CommentAdapter(Context context, List<Comment> list) {
        super(context, list);
    }

    @Override
    protected AbsViewHolder createHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(mInflater.inflate(R.layout.layout_post_comment_item, parent, false));
    }

    @Override
    protected void showViewHolder(AbsViewHolder holder, int position) {
        final ItemViewHolder viewHolder = (ItemViewHolder) holder;
        if (mData.get(position).getUser() != null) {
            BmobFile profile = mData.get(position).getUser().getProfile();
            if (profile == null) {
                viewHolder.userProfile.setImageResource(R.drawable.ic_load_error);
            } else {
                String profileUrl = profile.getUrl();
                Picasso.with(mContext)
                        .load(profileUrl)
                        .error(R.drawable.ic_load_error)
                        .into(viewHolder.userProfile);
            }
            viewHolder.userName.setText(mData.get(position).getUser().getNickName());
        }
        viewHolder.publishTime.setText(DateUtils.formatDate(mData.get(position).getCreatedAt()));
        viewHolder.publishContent.setText(mData.get(position).getContent());
        viewHolder.floor.setText("第" + (position + 1) + "楼");
    }

    private static class ItemViewHolder extends AbsViewHolder {

        ImageView userProfile;
        TextView userName;
        TextView publishTime;
        TextView publishContent;
        TextView floor;

        ItemViewHolder(View itemView) {
            super(itemView);
            userProfile = (ImageView) itemView.findViewById(R.id.id_follow_user_profile);
            userName = (TextView) itemView.findViewById(R.id.id_post_comment_author);
            publishTime = (TextView) itemView.findViewById(R.id.id_post_comment_time);
            publishContent = (TextView) itemView.findViewById(R.id.id_post_comment_content);
            floor = (TextView) itemView.findViewById(R.id.id_post_comment_floor);
        }
    }

}
